<?php include("includes/header.php"); ?>

<?php

if(!isset($_SESSION['email']))
{
  if(!headers_sent())
  {
    header('Location: login.php?redirect=true');
  }
  else
  {
    echo "<script>window.location.replace('login.php?redirect=true');</script>";
  }
}

?>

<?php

if(isset($_GET['vineyard']) && is_numeric($_GET['vineyard']))
{
  $vineyard_id = $_GET['vineyard'];

  $q = "SELECT * FROM Vineyard WHERE id=" . $vineyard_id . ";";
  $r = mysqli_query($dbc, $q);
  $row = mysqli_fetch_array($r, MYSQLI_NUM);
  
  if($row == NULL)
  {
    echo "There must be some kind of mistake!  We can't find that vineyard
          in our records.";
  }
  else
  {
    $id = $row[0];
    $name = $row[1];
    $address = $row[2];
    $city = $row[4];
    $state = $row[5];
    $zip = $row[6];
    $phone = substr($row[7], 0, 3) 
             . "-" . substr($row[7], 3, 3) 
             . "-" . substr($row[7], 6);
    $gmap = $row[8];  

    /* Make DB call to get review scores then calculate a percentage */
    $q = "SELECT review FROM Review WHERE vineyard_id= " . $id . ";";
    $r = mysqli_query($dbc, $q);
     
    $sum = 0;
    $row_count = 0;
    while($row = mysqli_fetch_row($r))
    {
      $row_count++;

      $sum += $row[0];
    }
    
    if($row_count > 0)
    {
      $average = (float)$sum / (float)$row_count;
      $average = sprintf('%0.1f', $average);
    }

    if(isset($_GET['review']))
    {
      $q = "SELECT comment, review FROM Review WHERE vineyard_id=" . $id . ";";
      $r = mysqli_query($dbc, $q);
      
      $i = 0;
      $reviews = array();
      while($row = mysqli_fetch_row($r))
      {
        $reviews[] = $row[0];
        $reviews[] = $row[1];
      }
    }
    
  }
}

function generateReviews($reviews)
{
  if(!empty($reviews))
  {
    for($i = 0; $i < count($reviews); $i += 2)
    {
      echo '<div class="review">';
      echo '<p class="reviewer_comment">"' . $reviews[$i] . '"</p>';
      echo '<div style="clear:both"></div>';
      echo '<p class="reviewer_score">Rating: ' . $reviews[$i + 1] . ' / 5</p>';
      echo '</div>';
    }
  }
}
?>

<!-- The content of this page will only load if a valid vineyard is found -->
<?php if(isset($id)){ ?>

<style>

  #vineyard_container
  {
    min-height: 345px;
    width: 900px;
    margin: 20px auto;
    overflow:auto;
  }
  #content_left
  {
    float: left;
    padding-left: 15px;
  }
  #content_left textarea
  {
    width: 435px;
    max-width: 435px;
    height: 150px;
  }
  #content_left form
  {
    font-size: 80%;
    margin-bottom: 15px;
  }
  #content_left input
  {
    margin-top: 5px;
    float: right;
  }
  #content_left select
  {
    margin-top: 5px;
  }
  #content_right
  {
    float: right;
  }

  #content_right iframe
  {
    border: 1px solid #5f0808;
  }
  #vineyard_container h4
  {
    color: #5f0808;
    margin-bottom: 10px;
  }
  
  #vineyard_container p
  {
    margin-top: -10px;
    font-size: 90%;
  }
  #vineyard_container a
  {
    font-size: 80%;
    color: #666;
  }
  
  .success
  {
    color: green;
  }
  .error
  {
    color: red;
  }
  .score
  {
    font-size: 80%;
    font-style: italic;
  }
  .review
  {
    border-top: 1px dotted gray; 
    border-bottom: 1px dotted gray;
    margin-left: 5px;
    margin-right: 5px;
    background-color: white;
    margin-bottom: 10px;
    overflow:auto;
    max-width: 435px;
  }
  .reviewer_comment
  {
    padding-top: 15px;
    margin-left: 5px;
    font-style: italic;
  }
  .reviewer_score
  {
    float: right;
    margin-right: 5px;
    margin-bottom: 5px;
  }
  
</style>
<div id="vineyard_container">
  <div id="content_left">
    <h4> <?php echo $name; ?> </h4>
    <p> 
      <?php echo $address; ?>
      <br />
      <?php echo $city . "," . $state . " " . $zip; ?>
      <br />
      <?php echo $phone ?>
    </p>
    
    <?php
    if(isset($_GET['msg']))
    {
      $msg = $_GET['msg'];
      
      if($msg == 0)
      {
        echo '<p class="error">You\'ve already reviewed this vineyard!</p>';
      }
      else
      {
        echo '<p class="success">Thanks for reviewing this vineyard!</p>';
      }
    }
    ?>

    <p>Rating: <span class="score"><?php if(isset($average)){ echo $average . " / 5"; } else { echo "No ratings yet!";} ?></span></p>
    
    <?php 
    
    if(isset($_GET['review'], $reviews))
    {
      generateReviews($reviews);
    }

    ?>

    <form action="php/review_handler.php" method="POST">
      <textarea name="comments" required autofocus placeholder="Review this vineyard!"></textarea>
      <p></p>
      <label>Rating: </label>
      <select name="rating">
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        <option value="4">4</option>
        <option value="5">5</option>
      </select>
      <input name="vineyard_id" type="hidden" value="<?php echo $id; ?>" />
      <input type="submit" value="Submit"></input><br />
    </form>
    <?php if(!isset($_GET['review'])){ ?>
    <a href="vineyard.php?vineyard=<?php echo $id; ?>&review=true">Read all reviews</a>
    <?php }else{ ?>
    <a href="vineyard.php?vineyard=<?php echo $id; ?>">Hide all reviews</a>
    <?php } ?>
  </div>
   
  <div id="content_right">
    <?php echo $gmap; ?>
  </div>
</div>

<?php } ?>
<!-- End of the conditional -->

<?php include("includes/footer.php"); ?>
